package paskal.ui.components;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Map;

import javax.swing.JButton;

import paskal.ui.Style;

public class Button extends JButton implements Component {
	private static final long serialVersionUID = -8874350156996887032L;

	public Button() {
		setOpaque(false);
		setBackground(new Color(0, 0, 0, 0));
		setDoubleBuffered(true);
		setBorderPainted(false);
		setContentAreaFilled(false);
		setFocusPainted(false);
		setCursor(new Cursor(Cursor.HAND_CURSOR));
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		super.paintComponent(g);
	}

	@Override
	public void stylize(String selector) {
		Style.getInstance().styleThisShit(selector, this);
	}

	public void setProperty(String key, Object value) {
		switch (key) {
		case "bounds": {
			setBounds((Rectangle) value);
			break;
		}
		case "color": {
			setForeground(new Color(Integer.parseInt(value.toString()
					.substring(1), 16)));
			break;
		}
		case "border": {
			@SuppressWarnings("unchecked")
			Map<String, Object> border = (Map<String, Object>) value;
			if (border.get("radius") != null) {
				// setBorderPainted(true);
				// (int) border.get("radius");
			}
			if (border.get("shadow") != null) {
				// setBorderPainted(true);

			}
			break;
		}
		case "font": {
			@SuppressWarnings("unchecked")
			Map<String, Object> font = (Map<String, Object>) value;
			Font f = new Font(font.get("name").toString(),
					(int) font.get("style"), (int) font.get("size"));
			setFont(f);
			break;
		}
		case "text": {
			setText(value.toString());
			break;
		}
		case "background": {
			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) value;

			String path = (String) background.get("image");
			if (path != null) {
				Point p = (Point) background.get("position");
				if (p != null) {
					Rectangle r = new Rectangle(p);
					r.height = getHeight();
					r.width = getWidth();
					setIcon(Style.getInstance().getImageIcon(path, r));
				} else {
					setIcon(Style.getInstance().getImageIcon(path));
				}
			}
			break;
		}
		case "hover": {
			@SuppressWarnings("unchecked")
			Map<String, Object> hover = (Map<String, Object>) value;
			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) hover
					.get("background");
			String path = (String) background.get("image");
			if (path != null) {
				Point p = (Point) background.get("position");
				if (p != null) {
					Rectangle r = new Rectangle(p);
					r.height = getHeight();
					r.width = getWidth();
					setRolloverIcon(Style.getInstance().getImageIcon(path, r));
				} else {
					setRolloverIcon(Style.getInstance().getImageIcon(path));
				}
			}
			break;
		}
		case "pressed": {
			@SuppressWarnings("unchecked")
			Map<String, Object> pressed = (Map<String, Object>) value;
			@SuppressWarnings("unchecked")
			Map<String, Object> background = (Map<String, Object>) pressed
					.get("background");
			String path = (String) background.get("image");
			if (path != null) {
				Point p = (Point) background.get("position");
				if (p != null) {
					Rectangle r = new Rectangle(p);
					r.height = getHeight();
					r.width = getWidth();
					setPressedIcon(Style.getInstance().getImageIcon(path, r));
				} else {
					setPressedIcon(Style.getInstance().getImageIcon(path));
				}
			}
			break;
		}
		default:
			break;
		}
	}

}